<?php
namespace Admin;

use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Session ;
use Illuminate\Support\Facades\Validator ;
use Illuminate\Support\Facades\Redirect ;
use Illuminate\Support\Facades\Hash ;

class UserController extends BaseController
{

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $users = \User::paginate(7);
        return View::make('admin.User.index')->with('users', $users) ;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('admin.User.create') ;
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        // validate
        // read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'username'       => 'required',
            'password'      => 'required',
            'email' => 'required|email'
        );
        $validator = Validator::make(Input::all(), $rules);
        
        // process the login
        if ($validator->fails()) {
            return Redirect::to('admin/user/create')->withErrors($validator);
        } else {
            // store
            $user = new \User();
            
            $user->username        =   Input::get('username');
            $user->password     =   Hash::make(Input::get('password')) ;
            $user->email  =   Input::get('email');
            
            $pid = $user->save(); 
            
            if ($pid) {
                // redirect
                Session::flash('message', ' 用户添加OK！');
                return Redirect::to('admin/user');
            }else{
                Session::flash('message', '用户添加失败！');
                return Redirect::to('admin/user/create');
            }
        }
        
    }

    /**
     * Display the specified resource.
     *
     * @param int $id            
     * @return Response
     */
    public function show($id)
    {
        $user = \User::find($id);
        return View::make('admin.User.show')->with('user', $user) ;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param int $id            
     * @return Response
     */
    public function edit($id)
    {
        $user = \User::find($id);
        return View::make('admin.User.edit')->with('user', $user) ;
    }

    /**
     * Update the specified resource in storage.
     *
     * @param int $id            
     * @return Response
     */
    public function update($id)
    {
         // validate
		// read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'username'       => 'required',
            //'password'      => 'required',
            'email' => 'required|email'
        );
		$validator = Validator::make(Input::all(), $rules);

		// process the login
		if ($validator->fails()) {
			return Redirect::to('admin/user/' . $id . '/edit')->withErrors($validator);
		} else {
			// store
			$user = \User::find($id);
			
			$user->username  = Input::get('username');
			//确定是否修改密码
			$pd = Input::get('password') ;
			if (!empty($pd)){
			    $user->password  = Hash::make(Input::get('password')) ;
			}
			
			$user->email     = Input::get('email');
			
			$pid = $user->save();
			
			// redirect
			Session::flash('message', '用户更新OK！');
			return Redirect::to('admin/user');
		}
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id            
     * @return Response
     */
    public function destroy($id)
    {

		$user = \User::find($id);
		$user->delete();

		// redirect
		Session::flash('message', '['.$user->username.'] - 已删除！');
		return Redirect::to('admin/user');
    }
}
